package com.elitel.common.businessJdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import com.elitel.common.exception.BusinessStatementException;

import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

// here is handle insert relative program
@Component
@Slf4j
public class BusinessInsertPreparedStatementHandler extends BusinessPreparedStatementHandler{

    // here is handle add new object by using
    // insert single row data
    public int insertSingleRowData(BusinessInsertPreparedStatement bps){
        // rows affect

        String precompileSql = bps.getPrecompileSql();
        SqlRecorder recorder = new SqlRecorder();
        recorder.setServiceGuid(bps.getServiceGuid());
        recorder.setPrecompileSql(precompileSql);

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int row = 0;

        try{

            connection = getConnection();
            preparedStatement = connection.prepareStatement(precompileSql);

            int parameterIndex = 1;
            for(Map.Entry<String, Object> columnEntry : bps.getColumnValueMap().entrySet()){
                String columnName = columnEntry.getKey();
                Object columnValue = columnEntry.getValue();
                String fieldType = bps.getFieldTypeMap().get(columnName);

                String parameterInfo = "index = " + parameterIndex + " contionItem: " + columnName + " = " + columnValue + " fieldType = " + fieldType;
                try{
                    if(matchPreparedStatementSetMethodByFieldType(bps.getDatabaseType(), preparedStatement, parameterIndex++, columnValue, fieldType)){
                        recorder.addParameterInformation(parameterInfo);
                    }else{
                        recorder.addParameterInformation("FAIL: " + parameterInfo);
                        recorder.addErrorMessage("FAIL: matchPreparedStatementSetMethodByFieldType match false, because of " + parameterInfo);
                    }
                }catch(Exception e){
                    recorder.addParameterInformation("FAIL: " + parameterInfo);
                    recorder.addErrorMessage(e.getMessage());
                }
            }

            if(recorder.canExecuteSql()){
                log.info(recorder.formatInfo());
            }else{
                log.error(recorder.formatInfo());
            }

            row = preparedStatement.executeUpdate();

            // close resource.
            preparedStatement.close();
            connection.close();

        }catch(Exception e){
            log.error("Insert Fail; " + e.getMessage());
            e.printStackTrace();

            try{
                if(preparedStatement != null) preparedStatement.close();
                if(connection != null) connection.close();
            }catch(SQLException sqlE){
                log.error(sqlE.getMessage());
            }

            throw new BusinessStatementException(e.getMessage(), recorder.formatInfo());
        }

        return row;
    }
}
